from django.contrib import admin
from xtversion.models import *


@admin.register(ProjectManage)
class ProjectAdmin(admin.ModelAdmin):
    list_display = ('project_name', 'creator', 'create_time')
    ordering = ('-create_time',)
    list_per_page = 20

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'creator':
            kwargs['initial'] = request.user.id
            kwargs['queryset'] = User.objects.filter(username=request.user.username)
        return super(ProjectAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)


@admin.register(VersionManage)
class VersionAdmin(admin.ModelAdmin):
    list_display = ('version_name', 'upload_date', 'creator', 'create_time')
    ordering = ('-create_time',)
    list_per_page = 20

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'creator':
            kwargs['initial'] = request.user.id
            kwargs['queryset'] = User.objects.filter(username=request.user.username)
        return super(VersionAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)


@admin.register(UploadHistoryManage)
class UploadHistoryAdmin(admin.ModelAdmin):
    list_display = (
        'version', 'project', '当晚支撑开发', '当晚支撑测试', '当晚支撑CMO', '早上支撑开发', '早上支撑测试', 'night_dedect_no', 'night_defect',
        'seven_defect_no', 'severn_defect', '创建人', 'create_time',
        'note')

    fieldsets = [
        ('基本信息', {'fields': ['version', 'project', 'night_developer', 'night_tester', 'night_cmo', 'morning_developer',
                             'morning_tester']}),
        ('上载问题', {'fields': ['night_dedect_no', 'night_defect', 'seven_defect_no', 'severn_defect']}),
        ('备注', {'fields': ['note']}), ('创建人', {'fields': ['creator']})]

    list_filter = ('version', 'project')

    search_fields = ('version__version_name', 'project__project_name', 'night_defect', 'severn_defect')

    # 外键显示
    def version(self, obj):
        return obj.version.version_name

    def project(self, obj):
        return obj.project.project_name

    def 当晚支撑开发(self, obj):
        return obj.night_developer.username + ' | ' + obj.night_developer.first_name

    def 当晚支撑测试(self, obj):
        return obj.night_developer.username + ' | ' + obj.night_developer.first_name

    def 当晚支撑CMO(self, obj):
        return obj.night_developer.username + ' | ' + obj.night_developer.first_name

    def 早上支撑开发(self, obj):
        return obj.night_developer.username + ' | ' + obj.night_developer.first_name

    def 早上支撑测试(self, obj):
        return obj.night_developer.username + ' | ' + obj.night_developer.first_name

    def 创建人(self, obj):
        return obj.night_developer.username + ' | ' + obj.night_developer.first_name

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'creator':
            kwargs['initial'] = request.user.id
            kwargs['queryset'] = User.objects.filter(username=request.user.username)
        return super(UploadHistoryAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
